﻿using System.Collections.Generic;
using ACompany.OrderProcessing.Services;
using ACompany.OrderProcessing.Model;
using StructureMap;

public class OrderListPresenter
{
	private IOrderListView _view;
	private IOrderService _OrderService;

    public OrderListPresenter(IOrderListView view)
        : this(view, ObjectFactory.GetInstance<IOrderService>())
	{
		
	}

	public OrderListPresenter(IOrderListView view, IOrderService orderService)
	{
		_view = view;
		_OrderService = orderService;
	}


	public void Display()
	{
		clearInfoAndErrorMessages();

		List<Order> orders;
		orders = _OrderService.GetAllOrdersBy(_view.OrderStatus);

		UpdateViewFrom(orders);

	}

	private void UpdateViewFrom(List<Order> orders)
	{
		_view.Orders = orders;
		displayInformationFrom(orders);
	}

	private void displayInformationFrom(List<Order> orders)
	{
		clearInfoAndErrorMessages();

		if (orders.Count == 0) {
			_view.InfoMessage(string.Format("There are no orders with the status '{0}'.", _view.OrderStatus.ToString()));
		}
	}

	private void clearInfoAndErrorMessages()
	{
		_view.ErrorMessage(string.Empty);
		_view.InfoMessage(string.Empty);
	}

}